<?php $no_left = true;?>
{include file='_header'}
<script type="text/javascript" src="__STATIC__/js/dist/area/cascade1.js"></script>
<script type="text/javascript" src="__STATIC__/js/lib/moment.js"></script>
<link rel="stylesheet" href="__STATIC__/components/datetimepicker/jquery.datetimepicker.css">
<link rel="stylesheet" href="__STATIC__/components/daterangepicker/daterangepicker.css">
<style type='text/css'>
    .tabs-container .form-group {overflow: hidden;}
    .tabs-container .tabs-left .panel-body {text-align:left;}
    .tab-goods .nav li { width:120px; text-align:center; }
    .tab-goods .nav li {float:left;}
    .spec_item_thumb {position:relative;width:30px;height:20px;padding:0;border-left:none;}
    .spec_item_thumb i { position:absolute;top:-5px;right:-5px; }
	.region-goods-details {
		background: #f8f8f8;
		margin-bottom: 10px;
		padding: 0 10px;
	}
	.region-goods-left{
		text-align: center;
		font-weight: bold;
		color: #333;
		font-size: 14px;
		padding: 20px 0;
	}
	.region-goods-right{
		border-left: 3px solid #fff;
		padding: 10px 10px;
	}
</style>
<div class="page-header">
    当前位置：
    <span class="text-primary">{if !empty($item['id'])}编辑{else}添加{/if}商品 <small>{if !empty($item['id'])}修改【{$item['title']}】{/if}</small></small>
    </span>
</div>
<div class="page-content">
	<div class="page-sub-toolbar">
		<span class=''>
			<a class="btn btn-primary btn-sm" href="{:url('merch/goods/add')}" >添加商品</a>
			<a class="btn btn-default btn-sm" href="{:url('merch/goods/sale',array('page'=>$_GET['page']))}">返回列表</a>
		</span>
	</div>
<form action="" method="post" class="form-horizontal form-validate" enctype="multipart/form-data">
    <input type="hidden" id="tab" name="tab" value="#tab_basic" />
    <div class="tabs-container tab-goods">
        <div class="tabs-left">
            <ul class="nav nav-tabs" id="myTab">
                <li {if empty($tab) || $tab=='basic'}class="active"{/if}><a href="#tab_basic">基本</a></li>
                <li {if $tab=='option'}class="active"{/if} ><a href="#tab_option">库存/规格</a></li>
                <li {if $tab=='param'}class="active"{/if} ><a href="#tab_param">参数</a></li>
                <li {if $tab=='des'}class="active"{/if} ><a href="#tab_des">详情</a></li>
                <li {if $tab=='buy'}class="active"{/if} ><a href="#tab_buy">购买权限</a></li>
                <li {if $tab=='sale'}class="active"{/if}  style="display: none;"><a href="#tab_sale">营销</a></li>
                <li {if $tab=='notice'}class="active"{/if} ><a href="#tab_notice" style="display: none;">下单通知</a></li>
				<li {if $tab=='verify'}class="active"{/if}><a href="#tab_verify" id="tab_mer_verify" {if !empty($item['virtualsend'])}style="display:none;"{/if} >线下核销</a></li>
            </ul>
			<div class="tab-content ">
                <div class="tab-pane   {if empty($tab) || $tab=='basic'}active{/if}" id="tab_basic"><div class="panel-body">{include file='goods/tab/basic'}</div></div>
                <div class="tab-pane  {if $tab=='option'}active{/if}" id="tab_option"> <div class="panel-body">{include file='goods/tab/option'}</div></div>
                <div class="tab-pane {if $tab=='param'}active{/if}" id="tab_param"> <div class="panel-body">{include file='goods/tab/param'}</div></div>
                <div class="tab-pane {if $tab=='des'}active{/if}" id="tab_des"> <div class="panel-body" style='padding:0;'>{include file='goods/tab/des'}</div></div>
                <div class="tab-pane {if $tab=='buy'}active{/if}" id="tab_buy"> <div class="panel-body">{include file='goods/tab/buy'}</div></div>
                <div class="tab-pane {if $tab=='sale'}active{/if}" id="tab_sale"  style="display: none;"> <div class="panel-body">{include file='goods/tab/sale'}</div></div>
                <div class="tab-pane  {if $tab=='notice'}active{/if}" id="tab_notice"  style="display: none;"> <div class="panel-body">{include file='goods/tab/notice'}</div></div>
                <div class="tab-pane {if $tab=='verify'}active{/if}" id="tab_verify" > <div class="panel-body">{include file='goods/tab/verify'}</div></div>
           	</div>
		</div>
	</div>
    <div class="form-group">
        <label class="col-sm-2 control-label"></label>
        <div class="col-sm-9 subtitle">
            <input type="submit" value="保存商品" class="btn btn-primary"/>
        </div>
    </div>
</form>
</div>
<script type="text/javascript">
	window.type = "{$item['type']}";
	window.virtual = "{$item['virtual']}";
        require(['bootstrap'], function () {
		$('#myTab a').click(function (e) {
			$('#tab').val( $(this).attr('href'));
			e.preventDefault();
			$(this).tab('show');
		})
	});
	$(function () {
        $(':radio[name=isverify]').click(function () {
            window.type = $("input[name='isverify']:checked").val();
            if (window.type == '2') {
                $(':checkbox[name=cash]').attr("checked",false);
                $(':checkbox[name=cash]').parent().hide();
            } else {
                $(':checkbox[name=cash]').parent().show();
            }
        })
        $(':radio[name=type]').click(function () {
            window.type = $("input[name='type']:checked").val();
            window.virtual = $("#virtual").val();
            if(window.type=='1'){
                $('.dispatch_info').show();
            } else {
                $('.dispatch_info').hide();
            }
            if (window.type == '2') {
                $('.send-group').show();
            } else {
                $('.send-group').hide();
            }
            if (window.type == '3') {
                if ($('#virtual').val() == '0') {
                    $('.choosetemp').show();
                }
            }
            if (window.type == '2' || window.type == '3') {
                $(':checkbox[name=cash]').attr("checked",false);
                $(':checkbox[name=cash]').parent().hide();
            } else {
                $(':checkbox[name=cash]').parent().show();
            }
        })
        $(":checkbox[name='buyshow']").click(function () {
            if ($(this).prop('checked')) {
                $(".bcontent").show();
            }
            else {
                $(".bcontent").hide();
            }
		})
        $(':radio[name=buyshow]').click(function () {
            window.buyshow = $("input[name='buyshow']:checked").val();
            if(window.buyshow=='1'){
                $('.bcontent').show();
            } else {
                $('.bcontent').hide();
            }
        })
    })
	window.optionchanged = false;
	$('form').submit(function(){
        var check = true;
        $(".tp_title,.tp_name").each(function(){
            var val = $(this).val();
            if(!val){
                $('#myTab a[href="#tab_diyform"]').tab('show');
                $(this).focus(),$('form').attr('stop',1),tip.msgbox.err('自定义表单字段名称不能为空!');
                check =false;
                return false;
            }
        });
        if(!check){return false;}
		window.type = $("input[name='type']:checked").val();
		window.virtual = $("#virtual").val();
		if ($("#goodsname").isEmpty()) {
			$('#myTab a[href="#tab_basic"]').tab('show');
			$('form').attr('stop',1);
            $(this).focus(),$('form').attr('stop',1),tip.msgbox.err('请填写商品名称!');
			return false;
		}
        var inum = 0;
        $('.gimgs').find('.img-thumbnail').each(function(){
            inum++;
        });
        if(inum == 0){
            $('#myTab a[href="#tab_basic"]').tab('show');
            $('form').attr('stop',1),tip.msgbox.err('请上传商品图片!');
            return false;
        }
		var full = true;
		if (window.type == '3') {
			if (window.virtual != '0') {  //如果单规格，不能有规格
				if ($('#hasoption').get(0).checked) {
					$('form').attr('stop',1),tip.msgbox.err('您的商品类型为：虚拟物品(卡密)的单规格形式，需要关闭商品规格！');
					util.message('您的商品类型为：虚拟物品(卡密)的单规格形式，需要关闭商品规格！');
					return false;
				}
			}
			else {
				var has = false;
				$('.spec_item_virtual').each(function () {
					has = true;
					if ($(this).val() == '' || $(this).val() == '0') {
						$('#myTab a[href="#tab_option"]').tab('show');
						$(this).next().focus();
						$('form').attr('stop',1),tip.msgbox.err('请选择虚拟物品模板!');
						full = false;
						return false;
					}
				});
				if (!has) {
					$('#myTab a[href="#tab_option"]').tab('show');
					$('form').attr('stop',1),tip.msgbox.err('您的商品类型为：虚拟物品(卡密)的多规格形式，请添加规格！');
					util.message('您的商品类型为：虚拟物品(卡密)的多规格形式，请添加规格！');
					return false;
				}
			}
		}
		if (!full) {
			return false;
		}
		full = checkoption();
        if (!full) {
            $('form').attr('stop',1),tip.msgbox.err('请输入规格名称!');
            return false;
        }
		if (optionchanged) {


			$('#myTab a[href="#tab_option"]').tab('show');


			$('form').attr('stop',1),tip.msgbox.err('规格数据有变动，请重新点击 [刷新规格项目表] 按钮!');


			return false;


		}


		var spec_item_title = 1;


		$(".spec_item").each(function (i) {


			var _this = this;


			if($(_this).find(".spec_item_title").length == 0){


				spec_item_title = 0;


			}


		});


		if(spec_item_title == 0){


			$('form').attr('stop',1),tip.msgbox.err('详细规格没有填写,请填写详细规格!');


			return false;


		}


		$('form').attr('stop',1);


		//处理规格


		optionArray();


		isdiscountDiscountsArray();


		$('form').removeAttr('stop');


		return true;


	});





	function optionArray()


	{


		var option_stock = new Array();


		$('.option_stock').each(function (index,item) {


			option_stock.push($(item).val());


		});





		var option_id = new Array();


		$('.option_id').each(function (index,item) {


			option_id.push($(item).val());


		});





		var option_ids = new Array();


		$('.option_ids').each(function (index,item) {


			option_ids.push($(item).val());


		});





		var option_title = new Array();


		$('.option_title').each(function (index,item) {


			option_title.push($(item).val());


		});





		var option_virtual = new Array();


		$('.option_virtual').each(function (index,item) {


			option_virtual.push($(item).val());


		});





		var option_marketprice = new Array();


		$('.option_marketprice').each(function (index,item) {


			option_marketprice.push($(item).val());


		});





		var option_productprice = new Array();


		$('.option_productprice').each(function (index,item) {


			option_productprice.push($(item).val());


		});





		var option_costprice = new Array();


		$('.option_costprice').each(function (index,item) {


			option_costprice.push($(item).val());


		});





		var option_goodssn = new Array();


		$('.option_goodssn').each(function (index,item) {


			option_goodssn.push($(item).val());


		});





		var option_productsn = new Array();


		$('.option_productsn').each(function (index,item) {


			option_productsn.push($(item).val());


		});





		var option_weight = new Array();


		$('.option_weight').each(function (index,item) {


			option_weight.push($(item).val());


		});





		var options = {


				option_stock : option_stock,


				option_id : option_id,


				option_ids : option_ids,


				option_title : option_title,


				option_marketprice : option_marketprice,


				option_productprice : option_productprice,


				option_costprice : option_costprice,


				option_goodssn : option_goodssn,


				option_productsn : option_productsn,


				option_weight : option_weight,


				option_virtual : option_virtual


		};


		$("input[name='optionArray']").val(JSON.stringify(options));


	}





	function isdiscountDiscountsArray()
	{
		{foreach name='levels' item='level'}
		var isdiscount_discounts_{$level['key']} = new Array();
		$(".isdiscount_discounts_{$level['key']}").each(function (index,item) {
			isdiscount_discounts_{$level['key']}.push($(item).val());
		});
		{/foreach}
		var isdiscount_discounts_id = new Array();
		$('.isdiscount_discounts_id').each(function (index,item) {
			isdiscount_discounts_id.push($(item).val());
		});
		var isdiscount_discounts_ids = new Array();


		$('.isdiscount_discounts_ids').each(function (index,item) {


			isdiscount_discounts_ids.push($(item).val());


		});





		var isdiscount_discounts_title = new Array();


		$('.isdiscount_discounts_title').each(function (index,item) {


			isdiscount_discounts_title.push($(item).val());


		});





		var isdiscount_discounts_virtual = new Array();


		$('.isdiscount_discounts_virtual').each(function (index,item) {


			isdiscount_discounts_virtual.push($(item).val());


		});





		var options = {


			{foreach name='levels' item='level'}


			isdiscount_discounts_{$level['key']} : isdiscount_discounts_{$level['key']},


			{/foreach}


			isdiscount_discounts_id : isdiscount_discounts_id,


			isdiscount_discounts_ids : isdiscount_discounts_ids,


			isdiscount_discounts_title : isdiscount_discounts_title,


			isdiscount_discounts_virtual : isdiscount_discounts_virtual


		};
		$("input[name='isdiscountDiscountsArray']").val(JSON.stringify(options));
	}
	function checkoption() {
        var full = true;
        var $spec_title = $(".spec_title");
        var $spec_item_title = $(".spec_item_title");
        if ($("#hasoption").get(0).checked) {
            if($spec_title.length==0){
                $('#myTab a[href="#tab_option"]').tab('show');
                full = false;
            }
            if($spec_item_title.length==0){
                $('#myTab a[href="#tab_option"]').tab('show');
                full = false;
            }
        }
        if (!full) {
            return false;
        }
        return full;
	}
    function type_change(type) {
        if(type == 2) {
            console.log($("input[name='virtualsend']:checked").val());
            if($("input[name='virtualsend']:checked").val()==1) {
                $("#tab_mer_verify").hide();
            }else{
                $("#tab_mer_verify").show();
            }
        }
    }
</script>
{include file='_footer'}


